from django.contrib import admin

# Register your models here.
from .models import *
from django.utils.html import format_html

admin.site.site_header = '新能源汽车销售平台'  # 页面头部的标题
admin.site.site_title = '新能源汽车销售平台后台管理'  # 浏览器标签页的标题
site_title="汽车"

#车
class CarsAdmin(admin.ModelAdmin):

    list_display = ('id','model_image','cname', 'price', 'type_name', 'model_name', 'detail_image')  # 使用新的方法名 'model_name'
    ordering = ('id',)  # 按照Cars的id字段降序排列
    list_filter = ('cname',)  # 添加一个基于订单状态的过滤器

    def type_name(self, obj):
        return obj.type.type_name

    type_name.short_description = '车型'

    def model_image(self, obj):

        return format_html('<img src="/{}" alt="{}" style="width:100px;height:auto;">', obj.curl,obj.cname)

    model_image.short_description = '展示图'  # 修改标题以反映内容

    def detail_image(self, obj):
        return format_html('<img src="/{}" alt="{}" style="width:100px;height:auto;">',obj.durl,obj.cname)

    detail_image.short_description = '实拍图'
    def model_name(self, obj):
        return obj.model.mname

    model_name.short_description = '品牌'  # 为该方法返回的列设置标题



#详情
class CardetailsAdmin(admin.ModelAdmin):
    list_display = ('id', 'payment', 'firm', 'rank', 'endurance', 'charge', 'car_name')
    ordering = ('id',)  # 按照Cars的id字段降序排列

    def car_name(self, obj):
        return obj.car.cname
    car_name.short_description = '车名'

#详情实拍图
class OfficeimgAdmin(admin.ModelAdmin):
    list_display = ('id', 'car_name', 'car_img')
    ordering = ('id',)

    def car_name(self, obj):
        return obj.car.cname
    car_name.short_description = '车名'

    def car_img(self, obj):
        return format_html('<img src="/{}"  style="width:100px;height:auto;">',obj.img)
    car_img.short_description = '实拍图集'

#车型
class CartypeAdmin(admin.ModelAdmin):
    list_display = ('id', 'type_name')
    ordering = ('id',)

#品牌
class CarmodelAdmin(admin.ModelAdmin):
    list_display = ('id', 'logo_img','mname')
    ordering = ('id',)
    def logo_img(self, obj):
        return format_html('<img src="/{}" style="width:50px;height:auto;">', obj.logo)
#评论
class EvaluateAdmin(admin.ModelAdmin):
    list_display = ('id','head_img','ename','evaluate','updated_at','cars_name')
    ordering = ('id',)

    def head_img(self, obj):
        return format_html('<img src="/{}" style="width:100px;height:auto;">', obj.head)

    head_img.short_description = '头像'
    def cars_name(self,obj):
        return obj.Cars.cname
    cars_name.short_description = '车名'

admin.site.register(Carmodels, CarmodelAdmin)
admin.site.register(Cartype, CartypeAdmin)
admin.site.register(Cars, CarsAdmin)
admin.site.register(Cardetails, CardetailsAdmin)
admin.site.register(Officeimg, OfficeimgAdmin)
admin.site.register(CarEvaluate, EvaluateAdmin)
